class register {
    constructor() {
        // 获取输入框
        this.hand = $('.user > input')
        // 提示信息
        this.p = $('.user > p')
        this.log = $('.user > b')
        this.init()
    }
    init() {
        this.call()
    }

    call(){
        this.verification(this.hand[0],this.p[0],/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,'请输入邮箱号') 
        this.verification(this.hand[1],this.p[1],/^(?=.*\d)(?=.*[A-z])[\da-zA-Z]{1,9}$/,'请输入密码') 
    
        this.verification(this.hand[2],this.p[2],/^(?=.*\d)(?=.*[A-z])[\da-zA-Z]{1,9}$/,'请输入确认密码',function hanSu(){
            let a = $('.user > input')[1].value
            let b = $('.user > input')[2].value
            let c = $('.user > input')[2]
            let p = $('.user > p')[2]
            if(a != b){
                console.log(a);
                c.nextSibling.className = 'sb'
                c.className = 'bor'
                p.classList = 'reds'
                p.innerHTML = '两次密码不一致'
            }
        }) 
    }
    // 验证
    // num--获取焦点或者失去焦点的元素
    //regular--正则表达式
    //tips--提示信息
    //ring--提示信息
    verification(num,tips,regular,string,Callback) {
            //注册条件 
            // console.log(3);
            let pig = false
            let obj = JSON.parse(localStorage.getItem('user')) || {}
            
            $('.sub>input')[0].onclick = ()=>{
               
                for (const key in obj) {
                    if(key == $('.user > input')[0].value){
                        if(confirm('此账号已注册，是否去登录？')){
                            location.href = '../html/logoin.html'
                        }
                    }
                    return
                }
                // console.log($('.xy>p>input')[0].checked);
       
                if(pig && $('.xy>p>input')[0].checked && $('.user > input')[1].value == $('.user > input')[2].value){
                    // let obj = {}
                    let pas = $('.user > input')[2].value
                    let key = $('.user > input')[0].value
                    obj[key] = pas
                    
                    localStorage.setItem('user',JSON.stringify(obj))
                    if(confirm('是否去登录？')){
                        location.href = '../html/logoin.html'
                    }
                }
               
            }
            num.onblur = (e) => {
            let nums = num.value
            let target = e.target || src.Element
            let re = regular
            if (re.test(nums)) {
                // 通过
                target.nextSibling.className = 'tg'
                pig = true
            } else {
                // sb = 失败
                target.nextSibling.className = 'sb'
                target.className = 'bor'
                tips.classList = 'reds'
                tips.innerHTML = string
            }
            Callback && Callback()
        }
        num.onfocus = (e)=>{
            let target = e.target || src.Element
            tips.classList = ''
            tips.innerHTML = ''
            target.className = ''
            target.nextSibling.className = ''
        }
    }
}
new register()